package com.app.view.zoon;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import com.app.view.R;

/**
 * Created by xkh on 2017/10/31.
 * 绑定页面，扩散动画
 */
public class FreezView extends View {
    private int maxRadius = 0;
    private Paint paint, paint2, paint3;
    private Handler handler = new Handler();
    private int firstCount = 40;
    private int sencondCount = 40;
    private int threeCount = 40;
    private int firstRedius;
    private int sencondRedius;
    private int threeRedius;
    private int alphaCount = 80;
    private int alphaCount2 = 80;
    private int alphaCount3 = 80;
    private boolean isStart = false;

    public FreezView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
        maxRadius = w / 3 + 120;
    }

    private void init() {
        paint = new Paint();
        // 设置抗锯齿
        paint.setAntiAlias(true);
        // 设置画笔颜色
        paint.setColor(getResources().getColor(R.color.color_fecb00));
        //设置画笔透明度
        paint.setAlpha(100);
        // 设置画笔要是，圆形/方形/其他
        // paint.setStrokeCap(Paint.Cap.BUTT);
        // 设置实心圆
        paint.setStyle(Paint.Style.FILL);
        // 设置画笔大小
        paint.setStrokeWidth(1);
        // 设置阴影
        // paint.setShadowLayer(5,5,5, Color.BLUE);
        // 设置为空心
        paint.setStyle(Paint.Style.FILL);

        paint3 = new Paint();
        // 设置抗锯齿
        paint3.setAntiAlias(true);
        // 设置画笔颜色
        paint3.setColor(getResources().getColor(R.color.color_fecb00));
        //设置画笔透明度
        paint.setAlpha(50);//paint.setAlpha(50);
        // 设置实心圆
        paint3.setStyle(Paint.Style.FILL);
        // 设置画笔大小
        paint3.setStrokeWidth(1);
        // 设置阴影
        // paint.setShadowLayer(5,5,5, Color.BLUE);
        // 设置为空心
        paint3.setStyle(Paint.Style.FILL);

        paint2 = new Paint();
        // 设置抗锯齿
        paint2.setAntiAlias(true);
        // 设置画笔颜色
        paint2.setColor(getResources().getColor(R.color.color_fecb00));
        //设置画笔透明度
        paint.setAlpha(10);// paint.setAlpha(10);
        // 设置实心圆
        paint2.setStyle(Paint.Style.FILL);
        // 设置画笔大小
        paint2.setStrokeWidth(1);
        // 设置阴影
        // paint.setShadowLayer(5,5,5, Color.BLUE);
        // 设置为空心
        paint2.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        if (!isStart)
            return;
        firstRedius = maxRadius * (41 - firstCount) / 40;
        sencondRedius = maxRadius * (41 - sencondCount) / 40;
        threeRedius = maxRadius * (41 - threeCount) / 40;

        if (firstRedius < maxRadius * 3 / 4) {
            paint.setAlpha(80);
        } else {
            //开始逐渐的减小透明度
            alphaCount -= 8;
            if (alphaCount > 0) {
                paint.setAlpha(alphaCount);
            } else {
                paint.setAlpha(0);
            }
        }

        if (firstCount < 20) {

            if (sencondRedius < maxRadius * 3 / 4) {
                paint2.setAlpha(80);
            } else {
                alphaCount2 -= 8;
                if (alphaCount2 > 0) {
                    paint2.setAlpha(alphaCount2);
                } else {
                    paint2.setAlpha(0);
                }
            }
            if (sencondCount > 1) {
                canvas.drawCircle(getWidth() / 2, getHeight() / 2,
                        sencondRedius, paint2);
            }

        }

        if (sencondCount < 20) {

            if (threeRedius < maxRadius * 3 / 4) {
                paint3.setAlpha(80);
            } else {
                alphaCount3 -= 8;
                if (alphaCount3 > 0) {
                    paint3.setAlpha(alphaCount3);
                } else {
                    paint3.setAlpha(0);
                }
            }
            if (threeCount > 1) {
                canvas.drawCircle(getWidth() / 2, getHeight() / 2, threeRedius, paint3);
            }

        }
        if (firstCount > 1) {
            canvas.drawCircle(getWidth() / 2, getHeight() / 2, firstRedius, paint);
        }


        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                if (firstCount > 1) {
                    firstCount--;
                    if (firstCount < 20) {
                        sencondCount--;
                    }
                    invalidate();
                } else if (sencondCount > 1) {
                    sencondCount--;
                    if (sencondCount < 20) {
                        threeCount--;
                    }
                    invalidate();
                } else if (threeCount > 1) {
                    threeCount--;
                    invalidate();
                } else {
                    handler.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            reSet();
                            invalidate();
                        }
                    }, 200);//第几遍的频率

                }

            }
        }, 0);//14

    }

    private void reSet() {
        firstCount = 40;
        sencondCount = 40;
        threeCount = 40;
        alphaCount = 100;
        alphaCount2 = 100;
        alphaCount3 = 100;

    }

    public void startAnima() {
        this.isStart = true;
        invalidate();
    }

    public boolean isAnima(){
        return isStart;
    }


    public void stopAnima() {
        this.isStart = false;
        invalidate();
    }

}
